/**
 * Created by zhanglihe on 15/11/7.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog',
    'datepicker'
], function ($, common, ready, Collection, template, Model, dialog, datepicker) {
    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            loading();
            this.fetch();
            this.event();
        },

        fetch: function () {
            var _this = this;
            this.collection = new Collection({
                url: _global.url.api + 'hot_share',
                condition: {type: '1'},
                model: {
                    event: {
                        '.edit_share:change': 'setShare',
                        '.edit_amount:change': 'setAmount'
                    },
                    setShare: function (e) {
                        e.preventDefault();
                        var id = this.data.id;
                        var share = $('input[name="edit_share_' + id + '"]').val();

                         var model =this;
                         new Model({
                         url: _global.url.api + 'hot_share',
                         data:{id:id,share:share,act:'share'}
                         }).put();
                        //使用商品基数接口
                        var data = {};
                        data.goods_id = this.data.goods_id;
                        data.pre_share_count = share;
                        data.is_enabled = 1;

                        if (new Model({
                                url: _global.url.api + 'goods_preset',
                                data: data
                            }).post()) {
                            _this.collection.fetch();
                        }
                    },
                    setAmount: function (e) {
                        e.preventDefault();
                        var id = this.data.id;
                        var amount = $('input[name="edit_amount_' + id + '"]').val();
                        var model = this;
                        if (new Model({
                                url: _global.url.api + 'hot_share',
                                data: {id: id, amount: amount, act: 'amount'}
                            }).put()) {
                        }
                        //使用商品基数接口
                        var data = {};
                        data.goods_id = this.data.goods_id;
                        data.pre_user_rebate = amount;
                        data.is_enabled = 1;

                        if (new Model({
                                url: _global.url.api + 'goods_preset',
                                data: data
                            }).post()) {
                            _this.collection.fetch();
                        }
                    }
                },
                callback: function () {
                    loaded();
                }
            });
            this.collection.fetch();
        },

        event: function () {
            this.data = {};
            var _this = this;
            //保存

        }
    };

    new index();

});